﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLite.Data;

namespace SolutionModel
{
    public static class DBConfig
    {
        //private static PurviewDBEntities DBContext {get;set;}

        //public static void SetDBContext()
        //{
        //    DBContext = new PurviewDBEntities();
        //}

        static readonly string DbContextKey = Guid.NewGuid().ToString();

        //单例模式
        public static PurviewDBEntities GetDBContext()
        {
            var dbContext = NLite.Threading.Local.Get(DbContextKey) as PurviewDBEntities;
            if (dbContext == null)
            {
                dbContext = new PurviewDBEntities();
                NLite.Threading.Local.Set(DbContextKey, dbContext);
            }

            return dbContext;
        }
        //单例模式
        public static PurviewDBEntities GetDBContext(DbConfiguration dbCon)
        {
            var dbContext = NLite.Threading.Local.Get(DbContextKey) as PurviewDBEntities;
            if (dbContext == null)
            {
                dbContext = new PurviewDBEntities();
                NLite.Threading.Local.Set(DbContextKey, dbContext);
            }

            return dbContext;
        }

        public static void Dispose()
        {
             var dbContext = NLite.Threading.Local.Get(DbContextKey) as PurviewDBEntities;
             if (dbContext != null)
                 dbContext.Dispose();
        }

    }
}
